package cn.b.sky.mes.cm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.cm.dao.CalendarPatternDao;
import cn.b.sky.mes.cm.model.CalendarPattern;
import cn.b.sky.mes.cm.model.CalendarPatternItem;
import cn.b.sky.mes.cm.model.condition.CalendarPatternCondition;
import cn.b.sky.mes.cm.service.CalendarPatternService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("calendarPatternService")
public class CalendarPatternServiceImpl implements CalendarPatternService {

  private static final Logger LOGGER = LoggerFactory.getLogger(CalendarPatternServiceImpl.class);

  @Resource
  CalendarPatternDao calendarPatternDao;

  @Override
  public List<CalendarPattern> selectCalendarPatternByCondition(CalendarPatternCondition calendarPatternCondition) {
    return calendarPatternDao.selectCalendarPatternByCondition(calendarPatternCondition);
  }

  @Override
  public int selectCalendarPatternCount(CalendarPatternCondition calendarPatternCondition) {
    return calendarPatternDao.selectCalendarPatternCount(calendarPatternCondition);
  }

  @Override
  public CalendarPattern selectCalendarPatternByCode(String siteCode, String calendarPatternCode) {
    return calendarPatternDao.selectCalendarPatternByCode(siteCode, calendarPatternCode);
  }

  @Override
  public void insertCalendarPattern(CalendarPattern calendarPattern) {
    calendarPatternDao.insertCalendarPattern(calendarPattern);
    if (calendarPattern.getItems() != null) {
      for (CalendarPatternItem item : calendarPattern.getItems()) {
        if (item.getDays() != null) {
          item.setSiteCode(calendarPattern.getSiteCode());
          item.setCalendarPatternCode(calendarPattern.getCalendarPatternCode());
          calendarPatternDao.insertCalendarPatternItem(item);
        }
      }
    }

  }

  @Override
  public void updateCalendarPattern(CalendarPattern calendarPattern) {
    calendarPatternDao.updateCalendarPattern(calendarPattern);
    calendarPatternDao.deleteCalendarPatternItem(calendarPattern.getSiteCode(),
        calendarPattern.getCalendarPatternCode());
    if (calendarPattern.getItems() != null) {
      for (CalendarPatternItem item : calendarPattern.getItems()) {
        if (item.getDays() != null) {
          item.setSiteCode(calendarPattern.getSiteCode());
          item.setCalendarPatternCode(calendarPattern.getCalendarPatternCode());
          calendarPatternDao.insertCalendarPatternItem(item);
        }
      }
    }

  }

  @Override
  public BatchJobReport deletetCalendarPattern(String siteCode, String calendarPatternCode) {
    String cause = "";
    Boolean success = true;
    try {
      calendarPatternDao.deleteCalendarPatternItem(siteCode, calendarPatternCode);
      calendarPatternDao.deltetCalendarPattern(siteCode, calendarPatternCode);
    } catch (Exception e) {
      LOGGER.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if (DatabaseUtils.isConstraintsError( e.getMessage())) {
        // 外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(),
            "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(calendarPatternCode, cause, success, SecurityContextUtil.getLanguageCode(),SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<CalendarPatternItem> selectCalendarPatternItems(String siteCode, String calendarPatternCode) {
    return calendarPatternDao.selectCalendarPatternItems(siteCode, calendarPatternCode);
  }

  @Override
  public List<CalendarPattern> selectUnLockCalendarPatternList(String siteCode) {
    return calendarPatternDao.selectUnLockCalendarPatternList(siteCode);
  }

}
